﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class BookManageForm : Form
    {
        public int CurEditIndex = -1;
        private string bookname;
        private string bookid;
        private string bookstate;
        private string operation;
        private string strsql;

        public BookManageForm()
        {
            InitializeComponent();
        }

        private void BookManageForm_Load(object sender, EventArgs e)
        {
            BookIdtextBox.Enabled = false;
            BookNametextBox.Enabled = false;
            BookStatetextBox.Enabled = false;
            Addbutton.Enabled = false;
            Changebutton.Enabled = false;
            Deletebutton.Enabled = false;
            BookdataGridView.Enabled = false;
        }

        private void StartSQLbutton_Click(object sender, EventArgs e)
        {
            BookIdtextBox.Enabled = true;
            BookNametextBox.Enabled = true;
            BookStatetextBox.Enabled = true;
            Addbutton.Enabled = true;
            Changebutton.Enabled = true;
            Deletebutton.Enabled = true;
            BookdataGridView.Enabled = true;

            strsql = "select bid, bookname, state from BookInfo";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            BookdataGridView.DataSource = dt;
            con.Close();
        }

        private void BookdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                CurEditIndex = e.RowIndex;
                BookIdtextBox.Text = BookdataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                BookNametextBox.Text = BookdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
                BookStatetextBox.Text = BookdataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
            }
        }

        private bool SearchIdSql()
        {
            bookid = BookIdtextBox.Text;
            strsql = "select bid from BookInfo where bid = '" + bookid + "'";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand(strsql,con);
            cmd.CommandText = strsql;
            object obj = cmd.ExecuteScalar();
            if (obj == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool SearchSql()
        {
            bookid = BookIdtextBox.Text;
            strsql = "select * from BookInfo";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand(strsql, con);
            cmd.CommandText = strsql;
            object obj = cmd.ExecuteScalar();
            if (obj != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void ChangeSql()
        {
            bookid = BookIdtextBox.Text;
            bookname = BookNametextBox.Text;
            bookstate = BookStatetextBox.Text;
            switch (operation)
            {
                case "Add":
                    strsql = "insert into BookInfo(bid, bookname, state)" +
                        " values('" + bookid + "','" + bookname + "','" + bookstate + "')";
                    break;
                case "Change":
                    try 
                    {
                        string id = BookdataGridView.Rows[CurEditIndex].Cells[0].Value.ToString();
                        strsql = "update BookInfo set bid='" + bookid + "'" + ",bookname='" +
                            bookname + "'" + ",state='" + bookstate + "'" + "where bid ='" + id + "'";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;
                case "Delete":
                    strsql = "delete from BookInfo where bid = '" + bookid + "'";
                    break;
            }
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = strsql;
            if (!string.IsNullOrEmpty(bookname) && !string.IsNullOrEmpty(bookid) && !string.IsNullOrEmpty(bookstate))
                try
                {
                    int count = cmd.ExecuteNonQuery();
                    if (count > 0)
                    {
                        this.BookIdtextBox.Text = "";
                        this.BookNametextBox.Text = "";
                        this.BookStatetextBox.Text = "";
                        MessageBox.Show("执行成功！");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("注册失败, " + ex.Message);
                }
            else
            {
                MessageBox.Show("信息为空格或空，执行错误！");
                this.BookIdtextBox.Text = "";
                this.BookNametextBox.Text = "";
                this.BookStatetextBox.Text = "";
            }
        }

        private void Addbutton_Click(object sender, EventArgs e)
        {
            operation = "Add";
            if (SearchIdSql())
            {
                ChangeSql();
            }
            else
            {
                MessageBox.Show("图书编号已被注册，执行错误！");
            }
        }

        private void Changebutton_Click(object sender, EventArgs e)
        {
            operation = "Change";
            if (SearchSql())
            {
                if (SearchIdSql())
                {
                    ChangeSql();
                }
                else
                {
                    MessageBox.Show("图书编号已被注册，执行错误！");
                }
            }
            else
            {
                MessageBox.Show("数据库为空，执行错误！");
            }
        }

        private void Deletebutton_Click(object sender, EventArgs e)
        {
            operation = "Delete";
            if (SearchSql())
            {
                ChangeSql();
            }
            else 
            {
                MessageBox.Show("数据库为空，执行错误！");
            }
        }

        private void Returnbutton_Click(object sender, EventArgs e)
        {
            Utils.SQLTool.CloseSql();
            this.Close();
        }
    }
}
